package com.etalk.crm.serviceImpl;

import com.etalk.crm.dao.UserMapper;
import com.etalk.crm.dao.YKNewMainMapper;
import com.etalk.crm.pojo.NewMainBean;
import com.etalk.crm.pojo.User;
import com.etalk.crm.pojo.UserPanelSort;
import com.etalk.crm.service.YKNewMainService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Auther: James
 * @Date: 2018/12/27 17:05
 * @Description: 约课顾问角色首页相关
 */
@Service
public class YKNewMainServiceImpl implements YKNewMainService {
    protected static final Logger logger = LogManager.getLogger(YKNewMainServiceImpl.class);

    @Resource
    private YKNewMainMapper ykNewMainMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public PageInfo<NewMainBean> selectTrackRemindByBccId(int bccId, String searchDate, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ykNewMainMapper.selectTrackRemindByBccId(bccId,searchDate));
    }

    @Override
    public PageInfo<NewMainBean> selectMainTipPotentialByBccId(int bccId, Integer state, Integer page, Integer size,String searchDate) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ykNewMainMapper.selectMainTipPotentialByBccId(bccId,state,searchDate));
    }

    @Override
    public PageInfo<NewMainBean> unfinishDemoCourseByBccId(int bccId, String searchDate, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ykNewMainMapper.unfinishDemoCourseByBccId(bccId,searchDate));
    }

    @Override
    public PageInfo<NewMainBean> demoFinishedListByBccId(int bccId, String searchDate, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(ykNewMainMapper.demoFinishedListByBccId(bccId,searchDate));
    }

    @Override
    public PageInfo<NewMainBean> demoMissedListByBccId(int bccId, String searchDate, Integer page, Integer size) {
       PageHelper.startPage(page, size);
        return new PageInfo<>(ykNewMainMapper.demoMissedListByBccId(bccId,searchDate));
    }

    @Override
    public List<UserPanelSort> selectSortByUser(int addUserId) {
        return ykNewMainMapper.selectSortByUser(addUserId);
    }

    @Transactional
    @Override
    public int addUserSort(List<UserPanelSort> userPanelSortList,int addUserId) {
        //查询用户规则是否存在
        List<UserPanelSort> sortList = ykNewMainMapper.selectSortByUser(addUserId);
        int result = 0;
        if(sortList != null && sortList.size()>0){
            result = ykNewMainMapper.delUserSort(addUserId);
            if(result >0){
                return ykNewMainMapper.addUserSort(userPanelSortList,addUserId);
            }
        }else{
             return ykNewMainMapper.addUserSort(userPanelSortList,addUserId);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> selectRevisitInfo() {
        return ykNewMainMapper.selectRevisitInfo();
    }

    /**
     * 查询门店cc
     * @param storesId 门店id
     * @return 用户列表
     */
    @Override
    public List<User> searchCcByStoresId(int storesId) {
        return userMapper.selectCcByStoresId(storesId);
    }

    /**
     * 查询门店ssc
     * @param storesId 门店id
     * @return 用户列表
     */
    @Override
    public List<User> searchSscByStoresId(int storesId) {
        return userMapper.selectSscByStoresId(storesId);
    }

    /**
     * 查询所有SSC和cc
     * @return 用户列表
     */
    @Override
    public List<User> searchCdByStoresId() {
        return userMapper.selectCcOrSscByStoresId();
    }
}
